package org.vaadinpa.gui;

import java.util.Iterator;

import org.vaadinpa.gui.views.AdminView;
import org.vaadinpa.gui.views.AsteroidsView;
import org.vaadinpa.gui.views.FleetView;
import org.vaadinpa.gui.views.NewsView;
import org.vaadinpa.gui.views.PlanetScanView;
import org.vaadinpa.gui.views.ResearchView;
import org.vaadinpa.gui.views.ShipView;
import org.vaadinpa.gui.views.UniverseView;
import org.vaadinpa.gui.views.WelcomeView;
import org.vaadinpa.logic.CommandHandler;
import org.vaadinpa.logic.commands.TickCommand;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

public class Menu extends VerticalLayout implements ClickListener {

	private static final long serialVersionUID = 1826134211687095424L;
	private MainLayout main;

	public Menu(MainLayout main) {
		this.main = main;

		Button b = new Button("Welcome", this);
		b.setData(WelcomeView.class);
		b.addStyleName("selected");
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("News", this);
		b.setData(NewsView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Asteroids", this);
		b.setData(AsteroidsView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Tech Level", this);
		b.setData(ResearchView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Planet scans", this);
		b.setData(PlanetScanView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Universe", this);
		b.setData(UniverseView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Ships", this);
		b.setData(ShipView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Fleets", this);
		b.setData(FleetView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		b = new Button("Admin", this);
		b.setData(AdminView.class);
		b.addStyleName(BaseTheme.BUTTON_LINK);
		addComponent(b);

		Button tick = new Button("TICK");
		tick.addListener(new ClickListener() {

			private static final long serialVersionUID = 6643574960693045407L;

			public void buttonClick(ClickEvent event) {
				CommandHandler.handle(new TickCommand());
			}
		});
		addComponent(tick);

		setSpacing(true);
		setMargin(true);
		setWidth("150px");
	}

	@SuppressWarnings("unchecked")
	public void buttonClick(ClickEvent event) {

		Iterator<Component> i = getComponentIterator();
		while (i.hasNext()) {
			i.next().removeStyleName("selected");
		}
		event.getButton().addStyleName("selected");
		main.setCurrentView((Class<? extends AbstractView>) event.getButton().getData());
	}

}
